// admin/system/handler/sys_dict_type_handler.go
package handler

import (
	"go-admin1/admin/system/model"
	"go-admin1/admin/system/service"
	"go-admin1/internal/common/utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

// SysDictTypeHandler 字典类型处理器
type SysDictTypeHandler struct {
	dictTypeService service.SysDictTypeService
}

// NewSysDictTypeHandler 创建字典类型处理器实例
func NewSysDictTypeHandler(dictTypeService service.SysDictTypeService) *SysDictTypeHandler {
	return &SysDictTypeHandler{
		dictTypeService: dictTypeService,
	}
}

// Create 创建字典类型
func (h *SysDictTypeHandler) Create(c *gin.Context) {
	var dictType model.SysDictType
	if err := c.ShouldBindJSON(&dictType); err != nil {
		utils.FailResponse[string](c, "参数解析失败")
		return
	}

	if err := h.dictTypeService.Create(&dictType); err != nil {
		utils.FailResponse[string](c, "创建字典类型失败")
		return
	}
	utils.OkResponseNoDataWithMessage[string](c, "创建字典类型成功")
}

// GetByID 根据ID获取字典类型
func (h *SysDictTypeHandler) GetByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		utils.FailResponse[string](c, "参数解析失败")
		return
	}

	dictType, err := h.dictTypeService.GetByID(id)
	if err != nil {
		utils.FailResponse[string](c, "获取字典类型失败")
		return
	}
	utils.OkResponse(c, dictType)
}

// GetByDictType 根据字典类型获取字典类型
func (h *SysDictTypeHandler) GetByDictType(c *gin.Context) {
	dictType := c.Query("dictType")
	if dictType == "" {
		utils.FailResponse[string](c, "字典类型不能为空")

		return
	}

	dict, err := h.dictTypeService.GetByDictType(dictType)
	if err != nil {
		utils.FailResponse[string](c, "获取字典类型失败")
		return
	}
	utils.OkResponse(c, dict)

}

// Update 更新字典类型
func (h *SysDictTypeHandler) Update(c *gin.Context) {
	var dictType model.SysDictType
	if err := c.ShouldBindJSON(&dictType); err != nil {
		utils.FailResponse[string](c, "参数解析失败")

		return
	}

	if err := h.dictTypeService.Update(&dictType); err != nil {
		utils.FailResponse[string](c, "更新字典类型失败")
		return
	}
	utils.OkResponseNoDataWithMessage[string](c, "更新字典类型成功")
}

// Delete 删除字典类型
func (h *SysDictTypeHandler) Delete(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		utils.FailResponse[string](c, "参数解析失败")
		return
	}

	if err := h.dictTypeService.Delete(id); err != nil {
		utils.FailResponse[string](c, "删除字典类型失败")
		return
	}
	utils.OkResponseNoDataWithMessage[string](c, "删除字典类型成功")
}

// List 字典类型列表
func (h *SysDictTypeHandler) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	dictTypes, total, err := h.dictTypeService.List(page, pageSize)
	if err != nil {
		utils.FailResponse[string](c, "获取字典类型列表失败")
		return
	}
	resultData := map[string]interface{}{
		"list":  dictTypes,
		"total": total,
	}
	utils.OkResponse(c, resultData)
}

// ListAll 所有字典类型列表
func (h *SysDictTypeHandler) ListAll(c *gin.Context) {
	dictTypes, err := h.dictTypeService.ListAll()
	if err != nil {
		utils.FailResponse[string](c, "获取字典类型列表失败")
		return
	}
	utils.OkResponse(c, dictTypes)
}
